package com.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.beans.Cla;
import com.beans.Student;
import com.controller.form.StudentVo;
import com.service.IClaService;
import com.service.IStudentService;
import com.utils.PageUtils;
import com.utils.UUIDUtils;

@Controller
public class StudentController {
	
	// 注入studentService
	@Autowired
	private IStudentService studentService;
	@Autowired
	private IClaService claService;
	
	// 学生注册
	@RequestMapping("/stuRegister.action")
	public String register(Student student,MultipartFile headImg){
		if (headImg!=null) {
			String filename = headImg.getOriginalFilename(); //获取文件名字
			filename = UUIDUtils.getUUID()+filename; //设置新名字
			File file = new File("/usr/local/stuImg/"+filename);
			try {
				headImg.transferTo(file);//写入文件
				student.setHead(filename); //重新设置头像
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		studentService.register(student);
		return "redirect:getStuListByPage.action?pageindex=1";
	}
	
	// 获取分页数据
	@RequestMapping("/getStuListByPage.action")
	public ModelAndView getStuListByPage(int pageindex){
		PageUtils<Student> pageUtils = studentService.getStuListByPage(pageindex);
		ModelAndView mv = new ModelAndView();
		mv.addObject("pageUtils", pageUtils);
		mv.setViewName("list");
		return mv;
	}
	
	// 普通删除学生
	@RequestMapping("/deleteStu.action")
	public String deleteStu(int id){
		studentService.deleteStu(id);
		return "redirect:getStuListByPage.action?pageindex=1";
	}
	
	// 获取学生详细信息
	@RequestMapping("/getStuById.action")
	public ModelAndView getStuById(int id){
		Student stu = studentService.getStuById(id);
		List<Cla> list = claService.getAllList(); //获取全部班级数据
		ModelAndView mv = new ModelAndView();
		mv.addObject("stu", stu);
		mv.addObject("clist", list);
		mv.setViewName("stuUpd");
		return mv;
	}
	
	// 修改学生
	@RequestMapping("/stuUpdate.action")
	public String stuUpdate(Student student,MultipartFile headImg){
		if (headImg!=null) {
			String filename = headImg.getOriginalFilename(); //获取文件名字
			filename = UUIDUtils.getUUID()+filename; //设置新名字
			File file = new File("G:/stuImg/"+filename);
			try {
				headImg.transferTo(file);//写入文件
				student.setHead(filename); //重新设置头像
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} 
		}
		studentService.stuUpdate(student);
		return "redirect:getStuListByPage.action?pageindex=1";
	}
	
	// 根据条件查询学生
	@RequestMapping("/getStuByLike.action")
	public ModelAndView getStuByLike(StudentVo student,int pageindex){
		ModelAndView mv = new ModelAndView();
		mv.addObject("svo", student);
		student.setPageindex(pageindex);
		PageUtils<Student> pageUtils = studentService.getStuByLike(student);
		mv.addObject("pageUtils", pageUtils);
		mv.setViewName("likeList");
		return mv;
	}
	// ajax修改学生
	@RequestMapping("/ajaxUpdStu.action")
	public void ajaxUpdStu(Student student){
		System.out.println();
		studentService.stuUpdate(student);
	}
}
